import Event from "sap/ui/base/Event";
import { BaseController } from "sap/ui/demo/nav/controller/BaseController";

/**
 * @namespace sap.ui.demo.nav.controller.employee
 */
export default class Employee extends BaseController {
  onInit(): void {
    this.router.getRoute("employee")?.attachMatched(this._routerMatched, this);
  }

  private _routerMatched(oEvent: Event) {
    const oArgs: { employeeId: number } = oEvent.getParameter("arguments");
    const oView = this.view;

    oView?.bindElement({
      path: `/Employees(${oArgs.employeeId})`,
      events: {
        change: this._onBindingChange.bind(this),
        dataRequested: () => {
          oView.setBusy(true);
        },
        dataReceived: () => {
          oView.setBusy(false);
        },
      },
    });
  }

  private _onBindingChange() {
    if (!this.view?.getBindingContext()) {
      this.router.getTargets()?.display("notFound");
    }
  }

  onShowResume() {
    const oCtx = this.view?.getElementBinding().getBoundContext();
    this.router.navTo("employeeResume", {
      employeeId: oCtx?.getProperty("EmployeeID"),
    });
  }
}
